<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">
        <p-row>
		  <p-col  :span="24">
			<p-form-item label="标签模板编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<f-popup
						contextPath="mes-code"
						v-decorator="['labelTemplateNo', validatorRules.labelTemplateNo]"
						:trigger-change="true"
						org-fields="id,label_template_no,label_template_name"
						dest-fields="label_template_id,label_template_no,label_template_name"
						code="mes_get_barcode_template"
						@callback="popupCallback"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="标签模板名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input readOnly v-decorator="[ 'labelTemplateName', validatorRules.labelTemplateName]" placeholder="请输入标签模板名称"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="工单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'productionno', validatorRules.productionno]" placeholder="请输入工单号"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="首标签条码" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'beginBarcode', validatorRules.beginBarcode]" placeholder="请输入首标签条码"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="尾标签条码" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'endBarcode', validatorRules.endBarcode]" placeholder="请输入尾标签条码"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="条码规则名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<f-popup
						contextPath="mes-code"
						v-decorator="['ruleName', validatorRules.ruleName]"
						:trigger-change="true"
						org-fields="id,rule_name"
						dest-fields="rule_id,rule_name"
						code="mes_get_barcode_rule"
						@callback="popupCallback"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input-number   v-decorator="[ 'qty', validatorRules.qty]" placeholder="请输入数量" style="width: 100%"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="打印人" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'name', validatorRules.name]" placeholder="请输入打印人"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="打印时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-date    placeholder="请选择打印时间" v-decorator="[ 'dt', validatorRules.dt]" :trigger-change="true" style="width: 100%"/>
			</p-form-item>
		  </p-col>
        </p-row>


      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import { SubmitOnchangMixin } from '@/mixins/SubmitOnchangMixin'
  import pick from 'lodash.pick'
  import JDate from '@/components/jeecg/JDate'

  export default {
    name: "MesBcPrintModal",
	mixins: [SubmitOnchangMixin],
    components: {
      JDate,
    },
    props:{
      mainId:{
        type:String,
        required:false,
        default:''
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
        labelTemplateNo: {rules: [{ required: true, message: '请输入标签模板编号!' }]},
        labelTemplateName: {},
        productionno: {rules: [{ required: true, message: '请输入工单号!' }]},
        beginBarcode: {rules: [{ required: true, message: '请输入首标签条码!' }]},
        endBarcode: {rules: [{ required: true, message: '请输入尾标签条码!' }]},
        ruleName: {rules: [{ required: true, message: '请输入条码规则名称!' }]},
        qty: {rules: [{ required: true, message: '请输入数量!' }]},
        name: {rules: [{ required: true, message: '请输入打印人!' }]},
        dt: {rules: [{ required: true, message: '请输入打印时间!' }]},
        },
        url: {
          add: '/mes-code/auto/mesBcPrintOrder/addMesBcPrint',
          edit: '/mes-code/auto/mesBcPrintOrder/editMesBcPrint',
        }

      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'timestamp','companyId','factoryId','clientId','substituteId','flag','labelTemplateId','labelTemplateNo','labelTemplateName','productionno','beginBarcode','endBarcode','ruleId','ruleName','qty','name','dt','mesBcPrintOrderId','remark','createBy','createTime','updateBy','updateTime'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
		handleOk () {
			const that = this;
			// 触发表单验证
			this.form.validateFields((err, values) => {
			  if (!err) {
				  that.confirmLoading = true;
				  let httpurl = '';
				  let method = '';
				  let formData = Object.assign(this.model, values);
				  this.formData = Object.assign(this.model, values);
                  formData['mesBcPrintOrderId'] = this.mainId
                  this.formData['mesBcPrintOrderId'] = this.mainId
				  //新增触发
				  if(!this.model.id){
					httpurl+=this.url.add;
					method = 'post';
					if(this.EnhanceJS.afterAdd){
					  //新增之前
					  let Obj = eval(this.EnhanceJS.afterAdd)
					  new Obj(this)
					}else{
					  console.log("表单提交数据",formData)
					  that.modalFrom(httpurl,formData,method)
					}
				  }else{
					//编辑触发
					httpurl+=this.url.edit;
					method = 'put';
					if(this.EnhanceJS.afterEdit){
					 //编辑之前
					  let Obj = eval(this.EnhanceJS.afterEdit)
					  new Obj(this)
					}else{
					  console.log("表单提交数据",formData)
					  that.modalFrom(httpurl,formData,method)
					}
				  }
			  }

			})
		},
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'timestamp','companyId','factoryId','clientId','substituteId','flag','labelTemplateId','labelTemplateNo','labelTemplateName','productionno','beginBarcode','endBarcode','ruleId','ruleName','qty','name','dt','mesBcPrintOrderId','remark','createBy','createTime','updateBy','updateTime'))
      },


    }
  }
</script>
